package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.FeeKey;
import com.atguigu.lease.model.entity.FeeValue;
import com.atguigu.lease.web.admin.service.FeeKeyService;
import com.atguigu.lease.web.admin.service.FeeValueService;
import com.atguigu.lease.web.admin.vo.fee.FeeKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Tag(name = "房间杂费管理")
@RestController
@RequestMapping("/admin/fee")
public class FeeController {

    @Autowired
    private FeeKeyService feeKeyService;
    @Autowired
    private FeeValueService feeValueService;

    @Operation(summary = "保存或更新杂费名称")
    @PostMapping("key/saveOrUpdate")
    public Result saveOrUpdateFeeKey(@RequestBody FeeKey feeKey) {
        boolean saveOrUpdate = feeKeyService.saveOrUpdate(feeKey);
        return Result.ok(saveOrUpdate);
    }

    @Operation(summary = "保存或更新杂费值")
    @PostMapping("value/saveOrUpdate")
    public Result saveOrUpdateFeeValue(@RequestBody FeeValue feeValue) {
        boolean saveOrUpdate = feeValueService.saveOrUpdate(feeValue);
        return Result.ok(saveOrUpdate);
    }


    @Operation(summary = "查询全部杂费名称和杂费值列表")
    @GetMapping("list")
    public Result<List<FeeKeyVo>> feeInfoList() {

    /* 方式一:
    ArrayList<FeeKeyVo> feeKeyVoArrayList = new ArrayList<>();

        List<FeeKey> feeKeyList = feeKeyService.list();
        for (FeeKey feeKey : feeKeyList) {
            FeeKeyVo feeKeyVo = new FeeKeyVo();
            LambdaQueryWrapper<FeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            feeValueLambdaQueryWrapper.eq(FeeValue::getId,feeKey);
            List<FeeValue> feeValueList = feeValueService.list(feeValueLambdaQueryWrapper);

            BeanUtils.copyProperties(feeKey,feeKeyVo);
            feeKeyVo.setFeeValueList(feeValueList);
            feeKeyVoArrayList.add(feeKeyVo);
        }


        return Result.ok(feeKeyVoArrayList);*/
//        方式二
        List<FeeKeyVo> feeKeyVoList = feeKeyService.feeInfoList();
        return Result.ok(feeKeyVoList);
    }

    @Operation(summary = "根据id删除杂费名称")
    @DeleteMapping("key/deleteById")
    public Result deleteFeeKeyById(@RequestParam Long feeKeyId) {

        boolean saveOrUpdate = feeKeyService.removeById(feeKeyId);
        return Result.ok(saveOrUpdate);


    }

    @Operation(summary = "根据id删除杂费值")
    @DeleteMapping("value/deleteById")
    public Result deleteFeeValueById(@RequestParam Long id) {
        boolean saveOrUpdate = feeValueService.removeById(id);
        return Result.ok(saveOrUpdate);
    }
}
